﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dominio;
using System.Data.Objects;

namespace Persistencia
{
    public class VideoPersistencia
    {

        #region Mapping Entities

        public VIDEO GetVideoEntity(Video video)
        {
            VIDEO videoEntity = new VIDEO();
            videoEntity.TITULO = video.Titulo;
            videoEntity.FILE_PATH = video.FilePath;

            return videoEntity;
        }

        #endregion


        #region Mapping Domain


        public Video GetVideoDomain(VIDEO videoEntity)
        {
            Video video = new Video();
            video.Id = videoEntity.ID;
            video.Titulo = videoEntity.TITULO;
            video.FilePath = videoEntity.FILE_PATH;
           
            if (videoEntity.VIDEO_EVENTO!= null)
            {
                video.Eventos = new List<Evento>();
                foreach (EVENTO e in GetEventosByVideoID(videoEntity.ID))
                {
                 //   video.Eventos.Add(GetEventoDomain(e));
                }
            }
            return video;
        }

        #endregion

        #region Video

        public void InsertarVideo(Video video)
        {
            try
            {

                VIDEO videoEntity = GetVideoEntity(video);
                using (OlimpycsLondonEntities entities = new OlimpycsLondonEntities())
                {
                    entities.VIDEO.AddObject(videoEntity);
                    entities.SaveChanges();                    
                }                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public void EliminarVideo(Video video)
        {

            using (OlimpycsLondonEntities context = new OlimpycsLondonEntities())
            {
                try
                {
                    VIDEO videoEntity = context.VIDEO.Include("VIDEO_EVENTO").FirstOrDefault(v => v.ID == video.Id);
                    context.DeleteObject(videoEntity);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public void ModificarVideo(Video video)
        {
            using (OlimpycsLondonEntities context = new OlimpycsLondonEntities())
            {
                var videoEntity = context.VIDEO.Include("VIDEO_EVENTO").FirstOrDefault(v => v.ID == video.Id);
                videoEntity.TITULO= video.Titulo;
                videoEntity.FILE_PATH= video.FilePath;

                //Elimino todas las relaciones video_evento
                List<VIDEO_EVENTO> listaVideoEvento =videoEntity.VIDEO_EVENTO.ToList<VIDEO_EVENTO>();
                foreach (VIDEO_EVENTO video_evento in listaVideoEvento)
                {
                    videoEntity.VIDEO_EVENTO.Remove(video_evento);
                }

                // inserto nuevamente todos las relaciones video_evento en funcion de los nuevos eventos asociados a los videos
                foreach (Evento evento in video.Eventos)
                {
                    InsertVideoEvento(videoEntity.ID, evento.Id);

                }

                context.SaveChanges();

            }
        }

        public Video GetVideo(string path)
        {

            Video video= null;

            using (OlimpycsLondonEntities entities = new OlimpycsLondonEntities())
            {
                VIDEO videoDB = entities.VIDEO.FirstOrDefault((v=> v.FILE_PATH== path));

                if (videoDB != null)
                    video = GetVideoDomain(videoDB);
            }

            return video;
          
        }
       
        public List<Video> GetAllVideos()
        {

            List<Video> videos = new List<Video>();

            using (OlimpycsLondonEntities entities = new OlimpycsLondonEntities())
            {
                List<VIDEO> videosDB = entities.VIDEO.ToList();
                Video video = null;
                foreach (VIDEO videoDB in videosDB)
                {
                    video= GetVideoDomain(videoDB);
                    videos.Add(video);
                }
            }

            return videos;

        }


        #endregion

        #region Video Evento

        public List<EVENTO> GetEventosByVideoID(int videoID)
        {
            using (OlimpycsLondonEntities context = new OlimpycsLondonEntities())
            {
                try
                {
                    var s = from e in context.EVENTO
                            join ve in context.VIDEO_EVENTO
                            on e.ID equals ve.EVENTO
                            where ve.VIDEO == videoID
                            select e;

                    return s.ToList();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }

        }

        public void InsertVideoEvento(int idVideo, int idEvento)
        {
            using (OlimpycsLondonEntities context = new OlimpycsLondonEntities())
            {
                VIDEO_EVENTO videoEventoDB = ((from ve in context.VIDEO_EVENTO
                                               where ve.VIDEO == idVideo && ve.EVENTO == idEvento
                                               select ve) as ObjectQuery<VIDEO_EVENTO>).SingleOrDefault();
                if (videoEventoDB == null)
                {
                    VIDEO_EVENTO ve = new VIDEO_EVENTO();
                    ve.EVENTO = idEvento;
                    ve.VIDEO = idVideo;
                    context.VIDEO_EVENTO.AddObject(ve);
                    context.SaveChanges();
                }
            }            
        }

        public void EliminarVideoEvento(int idVideo, int idEvento)
        {
            using (OlimpycsLondonEntities entities = new OlimpycsLondonEntities())
            {
                VIDEO_EVENTO videoEventoDB = ((from ve in entities.VIDEO_EVENTO
                                               where ve.VIDEO == idVideo && ve.EVENTO == idEvento
                                               select ve) as ObjectQuery<VIDEO_EVENTO>).SingleOrDefault();

                entities.VIDEO_EVENTO.DeleteObject(videoEventoDB);

                entities.SaveChanges();
            }
        }

        public List<Video> GetVideosByEvento(int idEvento)
        {
            List<Video> videos = new List<Video>();
            using (OlimpycsLondonEntities context = new OlimpycsLondonEntities())
            {
                List<VIDEO> videosDB = ((from v in context.VIDEO
                                               join ve in context.VIDEO_EVENTO on v.ID equals ve.VIDEO
                                               where ve.EVENTO == idEvento
                                               select v) as ObjectQuery<VIDEO>).ToList();

                Video video = null;
                foreach (VIDEO videoDB in videosDB)
                {
                    video = GetVideoDomain(videoDB);
                    videos.Add(video);
                }
            }

            return videos;
        }

        #endregion
    }
}
